package participation.Impl;

import cn.hutool.core.util.StrUtil;
import participation.dao.YzxParticipantDao;
import participation.domain.YzxParticipant;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class YzxParticipantDaoImpl implements YzxParticipantDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(YzxParticipant yzxParticipant) {
        String sql = "insert into yzx_participant\n" +
                "        (id,studentId,begin,end,fund,work,name,teacherId,typeId,time,status,majorId) \n" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,yzxParticipant.getId());
            psmt.setLong(2,yzxParticipant.getStudentId());
            psmt.setDate(3,yzxParticipant.getBegin());
            psmt.setDate(4,yzxParticipant.getEnd());
            psmt.setDouble(5,yzxParticipant.getFund());
            psmt.setString(6,yzxParticipant.getWork());
            psmt.setString(7,yzxParticipant.getName());
            psmt.setLong(8,yzxParticipant.getTeacherId());
            psmt.setLong(9,yzxParticipant.getTypeId());
            psmt.setDate(10,yzxParticipant.getTime());
            psmt.setInt(11,yzxParticipant.getStatus());
            psmt.setLong(12,yzxParticipant.getMajorId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long participant_id) {
        String sql = "delete from yzx_participant where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,participant_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(YzxParticipant yzxParticipant) {
        String sql = "update yzx_participant set";
        List<Object> param = new ArrayList<>();
        if (yzxParticipant == null) {
            return;
        } else {
            if (yzxParticipant.getId() != null) {
                sql += " id = ?,";
                param.add(yzxParticipant.getId());
            }
            if(yzxParticipant.getStudentId() != null) {
                sql += "studentId = ?,";
                param.add(yzxParticipant.getStudentId());
            }
            if(yzxParticipant.getBegin() != null) {
                sql += "begin = ?,";
                param.add(yzxParticipant.getBegin());
            }
            if(yzxParticipant.getEnd() != null) {
                sql += "end = ?,";
                param.add(yzxParticipant.getEnd());
            }
            if(yzxParticipant.getFund() != null) {
                sql += "fund = ?,";
                param.add(yzxParticipant.getFund());
            }
            if(yzxParticipant.getWork() != null) {
                sql += "work = ?,";
                param.add(yzxParticipant.getWork());
            }
            if(yzxParticipant.getName() != null) {
                sql += "name = ?,";
                param.add(yzxParticipant.getName());
            }
            if(yzxParticipant.getTeacherId() != null) {
                sql += "teacherId = ?,";
                param.add(yzxParticipant.getTeacherId());
            }
            if(yzxParticipant.getTypeId() != null) {
                sql += "typeId = ?,";
                param.add(yzxParticipant.getTypeId());
            }
            if(yzxParticipant.getTime() != null) {
                sql += "time = ?,";
                param.add(yzxParticipant.getTime());
            }
            if(String.valueOf(yzxParticipant.getStatus()) != null) {
                sql += "status = ?,";
                param.add(yzxParticipant.getStatus());
            }
            if(yzxParticipant.getMajorId() != null) {
                sql += "majorId = ?,";
                param.add(yzxParticipant.getMajorId());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,yzxParticipant.getId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public YzxParticipant selectById(Long id) {
        String sql = "select * from yzx_participant where id = ?";
        YzxParticipant yzxParticipant = new YzxParticipant();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                yzxParticipant.setId(rs.getLong(1));
                yzxParticipant.setStudentId(rs.getLong(2));
                yzxParticipant.setBegin(rs.getDate(3));
                yzxParticipant.setEnd(rs.getDate(4));
                yzxParticipant.setFund(rs.getDouble(5));
                yzxParticipant.setWork(rs.getString(6));
                yzxParticipant.setName(rs.getString(7));
                yzxParticipant.setTeacherId(rs.getLong(8));
                yzxParticipant.setTypeId(rs.getLong(9));
                yzxParticipant.setTime(rs.getDate(10));
                yzxParticipant.setStatus(rs.getInt(11));
                yzxParticipant.setMajorId(rs.getLong(12));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return yzxParticipant;
    }

    @Override
    public List<YzxParticipant> selectList(YzxParticipant yzxParticipant) {
        String sql = "select * from yzx_participant where 1=1";
        List<YzxParticipant> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (yzxParticipant == null) {
            sql = "select * from yzx_participant";
        } else {
            if(yzxParticipant.getId() != null) {
                sql += " and id = ?";
                param.add(yzxParticipant.getId());
            }
            if(yzxParticipant.getStudentId() != null) {
                sql += " and studentId = ?";
                param.add(yzxParticipant.getStudentId());
            }
            if(yzxParticipant.getBegin() != null) {
                sql += " and begin = ?";
                param.add(yzxParticipant.getBegin());
            }
            if(yzxParticipant.getEnd() != null) {
                sql += " and end = ?";
                param.add(yzxParticipant.getEnd());
            }
            if(yzxParticipant.getFund() != null) {
                sql += " and fund = ?";
                param.add(yzxParticipant.getFund());
            }
            if(yzxParticipant.getWork() != null) {
                sql += " and work = ?";
                param.add(yzxParticipant.getWork());
            }
            if(yzxParticipant.getName() != null) {
                sql += " and name = ?";
                param.add(yzxParticipant.getName());
            }
            if(yzxParticipant.getTeacherId() != null) {
                sql += " and teacherId = ?";
                param.add(yzxParticipant.getTeacherId());
            }
            if(yzxParticipant.getTypeId() != null) {
                sql += " and typeId = ?";
                param.add(yzxParticipant.getTypeId());
            }
            if(yzxParticipant.getTime() != null) {
                sql += " and time = ?";
                param.add(yzxParticipant.getTime());
            }
            // 用 Integer 可以和 null 比较
            if(yzxParticipant.getStatus() != null) {
                sql += " and status = ?";
                param.add(yzxParticipant.getStatus());
            }
            if(yzxParticipant.getMajorId() != null) {
                sql += " and majorId = ?";
                param.add(yzxParticipant.getMajorId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                YzxParticipant yzxParticipant1 = new YzxParticipant();
                yzxParticipant1.setId(rs.getLong(1));
                yzxParticipant1.setStudentId(rs.getLong(2));
                yzxParticipant1.setBegin(rs.getDate(3));
                yzxParticipant1.setEnd(rs.getDate(4));
                yzxParticipant1.setFund(rs.getDouble(5));
                yzxParticipant1.setWork(rs.getString(6));
                yzxParticipant1.setName(rs.getString(7));
                yzxParticipant1.setTeacherId(rs.getLong(8));
                yzxParticipant1.setTypeId(rs.getLong(9));
                yzxParticipant1.setTime(rs.getDate(10));
                yzxParticipant1.setStatus(rs.getInt(11));
                yzxParticipant1.setMajorId(rs.getLong(12));
                list.add(yzxParticipant1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
